import SysParamsSettingModel from "../../dao/model/system/SysParamsSettingModel";
import SysParamsSettingEntity, { ISysParamsSettingModel } from "../../entities/system/SysParamsSettingEntity";
import { IPageResponse } from "../../types/CommonType";

export class SysParamsSettingService {
  /**
   * 添加数据
   */
  public static async add(data: SysParamsSettingEntity): Promise<null> {
    await SysParamsSettingModel.create<ISysParamsSettingModel>(data);
    return null;
  }

  /**
   * 分页获取数据
   */
  public static async findByPage(current: number, size: number, where?: any): Promise<IPageResponse> {
    let data = await SysParamsSettingModel.findAndCountAll<ISysParamsSettingModel>({
      offset: size * (current - 1),
      limit: size,
      where,
    });

    const result: IPageResponse = {
      total: data.count,
      list: data.rows.map((item) => item.dataValues),
    };
    return result;
  }

  /**
   * 通过id获取数据
   */
  public static async findById(id: string) {
    const typeData = await SysParamsSettingModel.findByPk<ISysParamsSettingModel>(id);
    return typeData;
  }

  /**
   * 修改数据
   */
  public static async update(data: SysParamsSettingEntity) {
    await SysParamsSettingModel.update<ISysParamsSettingModel>(data, {
      where: {
        id: data.id,
      },
    });
    return null;
  }

  /**
   * 删除数据
   */
  public static async delete(id: string) {
    return SysParamsSettingModel.destroy({
      where: {
        id,
      },
    });
  }
}
